#include "storage.h"

Storage::Storage(QObject *parent)
    : QObject{parent}
{
    accessManager = new QNetworkAccessManager(this);
    connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");//构建json头
    //测试post
    //reply = accessManager->post(request, QByteArray("{\"password\": \"123456\", \"phone\": \"15683141454\"}"));
}
//测试成功
void Storage::storageAdd(QString token, QString amount, int foodId, int fridgeId, int userId)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/storage/add"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    QJsonObject json;
    json.insert("amount",amount);
    json.insert("foodId",foodId);
    json.insert("fridgeId",fridgeId);
    json.insert("userId",userId);
    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}
//一看就没错，懒得测试了
void Storage::storageDelete(QString token, QString alias, int amount, int foodId, int fridgeId, QString info, QString name, QString pic, QString type, QString unit, int userId)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/storage/delete"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    QJsonObject json;
    json.insert("alias",alias);
    json.insert("amount",amount);
    json.insert("foodId",foodId);
    json.insert("fridgeId",fridgeId);
    json.insert("info",info);
    json.insert("name",name);
    json.insert("pic",pic);
    json.insert("type",type);
    json.insert("unit",unit);
    json.insert("userId",userId);
    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}
//测试成功
void Storage::storageList(QString token, int filter)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/storage/list"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    QJsonObject json;
    json.insert("filter",filter);
    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}
//和delete一样，懒得测试
void Storage::storageUpdate(QString token, QString alias, int amount, int foodId, int fridgeId, QString info, QString name, QString pic, QString type, QString unit, int userId)
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/storage/update"));//设置url
    request.setRawHeader("Authorization",token.toLatin1());
    QJsonObject json;
    json.insert("alias",alias);
    json.insert("amount",amount);
    json.insert("foodId",foodId);
    json.insert("fridgeId",fridgeId);
    json.insert("info",info);
    json.insert("name",name);
    json.insert("pic",pic);
    json.insert("type",type);
    json.insert("unit",unit);
    json.insert("userId",userId);
    QJsonDocument jsonDocument;
    jsonDocument.setObject(json);
    postData = jsonDocument.toJson(QJsonDocument::Compact);
    reply = accessManager->post(request,postData);
}

void Storage::finishedSlot(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();
        //qDebug() << "许凡楠大帅哥";
        //qDebug() << bytes;
        QJsonParseError jsonError;
        QJsonDocument doucment = QJsonDocument::fromJson(bytes, &jsonError);  // 转化为 JSON 文档
        QJsonObject object = doucment.object();
        if (object.contains("code"))
        {
            QJsonValue value = object.value("code");  // 获取指定 key 对应的 value
            this->s_message.code=value.toInt();
        }
        if(object.contains("message"))
        {
            QJsonValue value = object.value("message");
            this->s_message.message=value.toString();
            qDebug()<<this->s_message.message;
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/storage/add")
        {
            //无返回
            emit storageAddSuccess();
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/storage/delete")
        {
            //无返回
            emit storageDeleteSuccess();
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/storage/update")
        {
            //无返回
            emit storageUpdateSuccess();
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/storage/list")
        {
            this->storagelist.clear();
            if(object.contains("data"))
            {
                QJsonValue value = object.value("data");
                if(value.isArray())
                {
                    QJsonArray array = value.toArray();
                    int nSize = array.size();
                    for(int i=0;i<nSize;i++)
                    {
                        QJsonValue value = array.at(i);
                        QJsonObject qobject = value.toObject();
                        storageInfo r_info;
                        if(qobject.contains("amount"))
                        {
                            QJsonValue value = qobject.value("amount");
                            r_info.amount=value.toInt();
                        }
                        if(qobject.contains("fridgeId"))
                        {
                            QJsonValue value = qobject.value("fridgeId");
                            r_info.fridgeId=value.toInt();
                        }
                        if(qobject.contains("foodId"))
                        {
                            QJsonValue value = qobject.value("foodId");
                            r_info.foodId=value.toInt();
                        }

                        if(qobject.contains("userId"))
                        {
                            QJsonValue value = qobject.value("userId");
                            r_info.userId=value.toInt();
                        }
                        if(qobject.contains("alias"))
                        {
                            QJsonValue value = qobject.value("alias");
                            r_info.alias=value.toString();
                        }

                        if(qobject.contains("info"))
                        {
                            QJsonValue value = qobject.value("info");
                            r_info.info=value.toString();
                        }
                        if(qobject.contains("name"))
                        {
                            QJsonValue value = qobject.value("name");
                            r_info.name=value.toString();
                        }
                        if(qobject.contains("pic"))
                        {
                            QJsonValue value = qobject.value("pic");
                            r_info.pic=value.toString();
                        }
                        if(qobject.contains("type"))
                        {
                            QJsonValue value = qobject.value("type");
                            r_info.type=value.toString();
                        }
                        if(qobject.contains("unit"))
                        {
                            QJsonValue value = qobject.value("unit");
                            r_info.unit=value.toString();
                        }
                        this->storagelist.push_back(r_info);
                    }
                }
            }
            emit storageListSuccess();
        }
    }
    else
    {
        qDebug() << "finishedSlot errors here";
        qDebug( "found error .... code: %d\n", (int)reply->error());
        qDebug(qPrintable(reply->errorString()));
    }
    reply->deleteLater();
}
